#include "threadPool.h"
int transFile(int netfd, char *str)
{
    train_t train;
    // 先把第一次发送的内容/文件名 装入小火车
    int existFlag = 0;
    off_t offset = 0;
    int fd;
    off_t filesize = 0;
    off_t sendSize=0;
    struct stat statbuf;
    train.length = strlen(str);
    memcpy(train.data, str, strlen(str));
    // 发送小火车
    send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    // 接受对面的信息，判断是否有filepart，并获取临时文件的大小
    recv(netfd, &existFlag, sizeof(existFlag), 0);
    if (existFlag == 1)
    {
        recv(netfd, &offset, sizeof(offset), 0); // 得到偏移值
        fd = open(str, O_RDWR);
        fstat(fd, &statbuf);
        lseek(fd, offset, SEEK_SET);
        sendSize = statbuf.st_size - offset;

        bzero(&train, sizeof(train));
        train.length = sizeof(sendSize);
        memcpy(train.data, &sendSize, sizeof(sendSize));
    }
    else
    {
        // 服务端先获取文件的大小 把文件大小装入小火车
        fd = open(str, O_RDWR);
        fstat(fd, &statbuf);
        bzero(&train, sizeof(train));
        train.length = sizeof(statbuf.st_size);
        memcpy(train.data, &statbuf.st_size, sizeof(statbuf.st_size));
        sendSize = statbuf.st_size - offset;
    }

    send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);                        // 获取文件大小
    printf("The total  sendSize is %lld bytes\n", (long long)sendSize); // 打印文件大小
    if (sendSize > 100 * 1024 * 1024)
    {
        // 先mmap，再send
        printf("mmap\n");
        char *p = (char *)mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        ERROR_CHECK(p, MAP_FAILED, "mmap");
        // 把映射区的内容分批send
        // 读取映射区的内容，发送到网络当中
        long totalSize = 0;
        while (totalSize < statbuf.st_size)
        {
            if (statbuf.st_size - totalSize > sizeof(train.data))
            {
                train.length = sizeof(train.data); // 如果未发送的长度大于1000，就发送1000
            }
            else
            {
                train.length = statbuf.st_size - totalSize;
            }
            // 设置火车车厢
            // memcpy(train.data,p+totalSize,train.length);
            // send(netfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
            send(netfd, &train, sizeof(train.length), MSG_NOSIGNAL);
            send(netfd, p + totalSize, train.length, MSG_NOSIGNAL);
            totalSize += train.length;
        }
        // 补发一个空车厢的小火车
        train.length = 0;
        send(netfd, &train, sizeof(train.length), MSG_NOSIGNAL);
    }
    else
    {
        // 传统的read和send
        ssize_t ret;
        while (1)
        {
            bzero(&train, sizeof(train));
            ret = read(fd, train.data, sizeof(train.data));
            train.length = ret; // 读取文件的片段放入小火车
            // 最后一次read read读完了，ret为0
            send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
            if (ret == 0)
            {
                break;
            }
        }
    }

    // //把映射区的一次性send过去
    // send(netfd,p,statbuf.st_size,MSG_NOSIGNAL);
    // munmap(p,statbuf.st_size);

    // 直接sendfile
    // sendfile(netfd,fd,NULL,statbuf.st_size);
    unsigned char md[16]="";
    md5(str,md);
    puts(md);
    for(int i = 0; i < 16;++i){
        printf("%02x", md[i]);
    }
    printf("\n");
    send(netfd,md,sizeof(md),0);
    close(netfd);
}